<template>
    <div class="CarUser-container content-wrapper">
        <div class="filter-container">
            <el-form :model="listQuery" :inline="true" ref="filterForm" style="display: inline-block">

                <el-form-item class="filter-item">
                    {{$route.params.realname}}
                </el-form-item>
                <el-form-item prop="carFullNum" class="filter-item">
                    <el-input v-model="listQuery.carFullNum" placeholder="车牌号" style="width: 200px;"></el-input>
                </el-form-item>

                <el-form-item prop="isHarm" class="filter-item">
                    <el-select v-model="listQuery.isHarm" placeholder="条件" style="width: 150px">
                        <el-option
                                v-for="item in filterOptions"
                                :key="item.key"
                                :label="item.label"
                                :value="item.key">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-button class="filter-item" type="primary" icon="search" @click="handleFilter">搜索</el-button>
                <el-button class="filter-item" type="primary" icon="mars-reset" @click="handleResetFilter">重置
                </el-button>

            </el-form>
        </div>

        <el-table :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row
                  style="width: 100%">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column label="ID" width="80" align="center">
                <template slot-scope="scope"> {{scope.row.id}} </template>
            </el-table-column>

            <el-table-column label="车牌号码" align="center">
                <template slot-scope="scope"> {{scope.row.carFullNum}} </template>
            </el-table-column>
            <el-table-column label="记录类型" align="center">
                <template slot-scope="scope">
                    <el-tag :type="scope.row.isHarm | isHarmTagFilter">{{scope.row.isHarm | isHarmFilter(scope.row.isHarm)}}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="处理前" width="250" align="left">
                <template slot-scope="scope">
                    <div style="width: 300px;padding: 10px" v-if="scope.row.isHarm == 1">
                        <template v-if="scope.row.beforeImageUrl">
                            <img :src="scope.row.beforeImageUrl" style="width: 200px; height: 100px;">
                            <p>备注信息：{{scope.row.beforeDesc!=="" ? scope.row.beforeDesc : ""}}</p>
                            <small>上传人：{{scope.row.beforeUploadUser!=="" ? scope.row.beforeUploadUser : ""}}</small><br>
                            <small>上传时间：{{scope.row.beforeTime!=="" ? scope.row.beforeTime : ""}}</small>
                        </template>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="处理后" width="250" align="left">
                <template slot-scope="scope">
                    <div style="width: 300px;padding: 10px" v-if="scope.row.isHarm == 1">
                        <template v-if="scope.row.afterImageUrl">
                            <img :src="scope.row.afterImageUrl" style="width: 200px; height: 100px;">
                            <p>备注信息：{{scope.row.afterDesc!=="" ? scope.row.afterDesc : ""}}</p>
                            <small>上传人：{{scope.row.afterUploadUser!=="" ? scope.row.afterUploadUser : ""}}</small><br>
                            <small>上传时间：{{scope.row.afterTime!=="" ? scope.row.afterTime : ""}}</small>
                        </template>

                    </div>
                </template>
            </el-table-column>
            <el-table-column label="车辆检查表" align="center">
                <template slot-scope="scope">
                    <el-button type="text" @click="handleCarCheckDetail(scope.row)">详情</el-button>
                </template>
            </el-table-column>
            <el-table-column label="赔偿情况" align="center">
                <template slot-scope="scope">
                    <span v-if="scope.row.compensate === undefined">未处理</span>
                    <span v-else-if="scope.row.compensate === 1">有赔偿</span>
                    <span v-else="">无赔偿</span>
                    <br>
                    <span v-if="scope.row.compensate !== undefined">
                        <span v-if="scope.row.compensateDesc === '' || scope.row.compensateDesc === undefined">无备注</span>
                        <span v-else="">备注：{{scope.row.compensateDesc}}</span>
                    </span>
                </template>
            </el-table-column>
            <el-table-column label="跟进状态" align="center">
                <template slot-scope="scope">
                    <el-tag :type="scope.row.follow | followTagFilter">{{scope.row.follow | followFilter(scope.row.follow, scope.row.isHarm)}}</el-tag>
                </template>
            </el-table-column>

            <el-table-column label="创建时间" width="150px" align="center">
                <template slot-scope="scope"> {{scope.row.createTime | parseTime('{y}-{m}-{d} {h}:{i}')}} </template>
            </el-table-column>
            <el-table-column label="更新时间" width="150px" align="center">
                <template slot-scope="scope"> {{scope.row.updateTime | parseTime('{y}-{m}-{d} {h}:{i}')}} </template>
            </el-table-column>

        </el-table>

        <div v-show="!listLoading" class="pagination-container">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="listQuery.page"
                    :page-sizes="[10, 20, 30, 50]"
                    :page-size="listQuery.rows"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

        <el-dialog :title="CarCheckDetailTitle" :visible.sync="dialogFormVisible" size="tiny">
            <el-form :model="carCheckForm" labelPosition="right" labelWidth="100px" style="width: 500px">

                <el-form-item label="类型">
                    <el-tag type="success" v-if="carCheckForm.wx">无伤</el-tag>
                    <el-tag type="danger" v-else="">新伤</el-tag>
                </el-form-item>
                <el-form-item label="轮胎">
                    <el-tag type="success" v-if="carCheckForm.c1 === 1">正常</el-tag>
                    <el-tag type="danger" v-else="">异常</el-tag>
                </el-form-item>
                <el-form-item label="外观">
                    <el-tag type="success" v-if="carCheckForm.c2 === 1">正常</el-tag>
                    <el-tag type="danger" v-else="">异常</el-tag>
                </el-form-item>
                <el-form-item label="灯光">
                    <el-tag type="success" v-if="carCheckForm.c3 === 1">正常</el-tag>
                    <el-tag type="danger" v-else="">异常</el-tag>
                </el-form-item>
                <el-form-item label="喇叭">
                    <el-tag type="success" v-if="carCheckForm.c4 === 1">正常</el-tag>
                    <el-tag type="danger" v-else="">异常</el-tag>
                </el-form-item>
                <el-form-item label="雨刮">
                    <el-tag type="success" v-if="carCheckForm.c5 === 1">正常</el-tag>
                    <el-tag type="danger" v-else="">异常</el-tag>
                </el-form-item>
                <el-form-item label="内饰">
                    <el-tag type="success" v-if="carCheckForm.c6 === 1">正常</el-tag>
                    <el-tag type="danger" v-else="">异常</el-tag>
                </el-form-item>
                <el-form-item label="发动机">
                    <el-tag type="success" v-if="carCheckForm.c7 === 1">正常</el-tag>
                    <el-tag type="danger" v-else="">异常</el-tag>
                </el-form-item>
                <el-form-item label="机油">
                    <el-tag type="success" v-if="carCheckForm.c8 === 1">正常</el-tag>
                    <el-tag type="danger" v-else="">异常</el-tag>
                </el-form-item>
                <el-form-item label="刹车油">
                    <el-tag type="success" v-if="carCheckForm.c9 === 1">正常</el-tag>
                    <el-tag type="danger" v-else="">异常</el-tag>
                </el-form-item>
                <el-form-item label="水箱水">
                    <el-tag type="success" v-if="carCheckForm.c10 === 1">正常</el-tag>
                    <el-tag type="danger" v-else="">异常</el-tag>
                </el-form-item>
                <el-form-item label="雨刮水">
                    <el-tag type="success" v-if="carCheckForm.c11 === 1">正常</el-tag>
                    <el-tag type="danger" v-else="">异常</el-tag>
                </el-form-item>
                <el-form-item label="备胎">
                    <el-tag type="success" v-if="carCheckForm.c12 === 1">正常</el-tag>
                    <el-tag type="danger" v-else="">异常</el-tag>
                </el-form-item>
                <el-form-item label="标贴">
                    <el-tag type="success" v-if="carCheckForm.c13 === 1">正常</el-tag>
                    <el-tag type="danger" v-else="">异常</el-tag>
                </el-form-item>
                <el-form-item label="三脚架">
                    <el-tag type="success" v-if="carCheckForm.c14 === 1">正常</el-tag>
                    <el-tag type="danger" v-else="">异常</el-tag>
                </el-form-item>
                <el-form-item label="千斤顶">
                    <el-tag type="success" v-if="carCheckForm.c15 === 1">正常</el-tag>
                    <el-tag type="danger" v-else="">异常</el-tag>
                </el-form-item>
                <el-form-item label="行驶证">
                    <el-tag type="success" v-if="carCheckForm.c16 === 1">正常</el-tag>
                    <el-tag type="danger" v-else="">异常</el-tag>
                </el-form-item>
                <el-form-item label="是否已加油">
                    <el-tag type="success" v-if="carCheckForm.c17 === 1">是</el-tag>
                    <el-tag type="danger" v-else="">否</el-tag>
                </el-form-item>
                <el-form-item label="地点">
                    {{carCheckForm.point}}
                </el-form-item>
                <el-form-item label="里程数">
                    {{carCheckForm.mileage}}
                </el-form-item>
                <el-form-item label="油量情况">
                    <el-tag type="success" v-if="carCheckForm.c18 === 1">满油</el-tag>
                    <el-tag type="success" v-else-if="carCheckForm.c18 === 2">3/4以上</el-tag>
                    <el-tag type="warning" v-else-if="carCheckForm.c18 === 3">1/2~3/4</el-tag>
                    <el-tag type="danger" v-else-if="carCheckForm.c18 === 4">1/4~1/2,5</el-tag>
                    <el-tag type="danger" v-else="">1/4以下</el-tag>
                </el-form-item>
                <el-form-item label="类型">
                    <el-tag type="success" v-if="carCheckForm.c7 === 1">正常</el-tag>
                    <el-tag type="danger" v-else="">异常</el-tag>
                </el-form-item>

            </el-form>

        </el-dialog>

    </div>
</template>

<script>
    import { created, deleted, updated, fetchObject, fetchList } from '@/api/tukuan/state'
    import { fetchObject as carCheckFetchObject } from '@/api/tukuan/check'

    const filterOptions = [{label: '新伤', key: '1'}, {label: '无伤', key: '2'}]

    export default {
        name: 'CarUser',
        data() {
            return {
                list: null,
                total: null,
                listLoading: true,
                dialogFormVisible: false,
                CarCheckDetailTitle: '',
                listQuery: {
                    page: 1,
                    rows: 10,
                    userId: this.$route.params.userId,
                    carFullNum: undefined,
                    createTime: undefined,
                    isHarm: '',
                    orderBy: 'create_time desc'
                },
                filterOptions,
                carCheckForm: {
                }
            }
        },
        created() {
            this.getList()
        },
        filters: {
            isHarmFilter(status) {
                const statusArray = ['新伤', '无伤']
                return statusArray[status?--status:'']
            },
            isHarmTagFilter(status) {
                const statusArray = ['danger', 'success']
                return statusArray[status?--status:'']
            },
            followFilter(status, isHarm) {
                if(status === undefined) {
                    return '未处理'
                } else {
                    if(status === 1) {
                        return '未跟进'
                    } else {
                        return '已跟进'
                    }
                }
            },
            followTagFilter(status) {
                if(status === undefined) {
                    return '未处理'
                } else {
                    if(status === 1) {
                        return 'danger'
                    } else {
                        return 'success'
                    }
                }
            }
        },
        methods: {
            getList() {
                this.listLoading = true
                fetchList(this.listQuery).then(response => {
                    this.list = response.data.list
                    this.total = response.data.total
                    this.listLoading = false
                }).catch((error)=>{
                    console.log(error)
                    this.listLoading = false
                })
            },
            handleCarCheckDetail(row) {
                if(row.carCheckId) {
                    this.CarCheckDetailTitle = row.carFullNum + ' - 车辆检查详情'
                    carCheckFetchObject(row.carCheckId).then(response => {
                        this.carCheckForm = Object.assign({}, response.data)
                    })
                    this.dialogFormVisible = true
                } else {
                    this.$notify({
                        title: '提示', message: '该车辆没有检查详情',
                        type: 'warning', duration: 2000
                    })
                }
            },
            handleSizeChange(val) {
                this.listQuery.rows = val
                this.getList()
            },
            handleCurrentChange(val) {
                this.listQuery.page = val
                this.getList()
            },
            handleCloseDialog() {
                this.resetForm()
            },
            handleFilter() {
                this.listQuery.page = 1
                this.getList()
            },
            handleResetFilter() {
                this.$refs.filterForm.resetFields()
                this.getList()
            },
            handleCreate() {
                this.dialogStatus = 'create'
                this.dialogFormVisible = true
            },
            handleUpdate(row) {
                this.dialogStatus = 'update'

                fetchObject(row.id).then(response => {
                    this.dialogFormVisible = true
                    this.form = Object.assign({}, response.data)
                })
            },
            handleDelete(row) {
                this.$confirm('确定要删除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleted(row.id).then(response => {
                        if(response.data.status) {
                            this.$notify({
                                title: '成功', message: response.data.message,
                                type: 'success', duration: 2000
                            })
                            this.getList()
                        } else {
                            this.$notify({
                                title: '发生错误', message: response.data.message,
                                type: 'error', duration: 2000
                            })
                        }
                    })
                }).catch(() => {
                    this.$message({ type: 'info', message: '已取消删除' })
                })
            },
            create() {
                this.$refs['form'].validate((valid) => {
                    if(valid) {
                        created(this.form).then(response => {
                            if(response.data.status) {
                                this.$notify({
                                    title: '成功', message: response.data.message,
                                    type: 'success', duration: 2000
                                })
                                this.getList()
                            } else {
                                this.$notify({
                                    title: '操作失败', message: response.data.message,
                                    type: 'error', duration: 2000
                                })
                            }
                            this.dialogFormVisible = false
                        })
                    }
                })

            },
            update() {
                this.$refs['form'].validate((valid) => {
                    if(valid) {
                        updated(this.form).then(response => {
                            if(response.data.status) {
                                this.$notify({
                                    title: '成功', message: response.data.message,
                                    type: 'success', duration: 2000
                                })
                                this.getList()
                            } else {
                                this.$notify({
                                    title: '发生错误', message: response.data.message,
                                    type: 'error', duration: 2000
                                })
                            }
                            this.dialogFormVisible = false
                        })
                    }
                })
            },
            resetForm() {
                this.$refs['form'].resetFields()
                this.form = {
                    city: '',
                    area: '',
                    realname: '',
                    gender: 1,
                    username: '',
                    mobile: '',
                    email: '',
                    status: 1,
                    remarks: ''
                }
            }
        }
    }
</script>

<!-- 局部样式 -->
<style rel="stylesheet/scss" lang="scss" scoped>

</style>